package com.multireactor;


import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * Reactor模式->一个接收现场  一个处理线程
 *
 */
public class MultiEchoServerReactor  {

    private Selector reqSel;

    private Selector workSel;
    private ServerSocketChannel server;

    public MultiEchoServerReactor(int port) throws Exception {
        //主要轮询监听
        reqSel=Selector.open();
        System.out.println("请求："+reqSel);
        //
        workSel=Selector.open();
        System.out.println("工作："+workSel);
        server=ServerSocketChannel.open();
        server.configureBlocking(false);
        server.socket().bind(new InetSocketAddress(port),1024);
        SelectionKey sk = server.register(reqSel, SelectionKey.OP_ACCEPT);
        sk.attach(new AcceptorHandler(server,workSel,reqSel));
        System.out.println("准备接收请求");
    }



    public void start() throws Exception {
        new Thread(new SubReactor(reqSel,server)).start();
        new Thread(new SubReactor(workSel,server)).start();

    }
}
